package xyz.zichengdulin.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.zichengdulin.pojo.PageResult;
import xyz.zichengdulin.pojo.Result;
import xyz.zichengdulin.pojo.Student;
import xyz.zichengdulin.pojo.StudentQueryParam;
import xyz.zichengdulin.service.StudentService;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学员条件分页查询
     *
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("学员列表分页查询:{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据ids批量删除学员
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) { //@PathVariable用于解析路径参数，@RequestParam用于解析请求参数
        log.info("删除学员信息:{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 添加学员
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody Student student) {
        log.info("添加学员:{}", student);
        studentService.insert(student);
        return Result.success();
    }

    /**
     * 根据id查询学生信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfoById(@PathVariable Integer id) {
        log.info("根据id查询学生信息:{}", id);
        Student getInfoByIdResult = studentService.getInfoById(id);
        return Result.success(getInfoByIdResult);
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("更新学员信息:{}", student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 修改学生违纪信息
     *
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violate(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("学生违纪信息处理:{},{}", id, score);
        studentService.violate(id, score);
        return Result.success();
    }


}
